package com.dingtalk;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.*;
import com.dingtalk.api.response.*;
import com.taobao.api.ApiException;

import java.util.HashMap;
import java.util.Map;

public class DingtalkClient {


    public static Map<String,String> map=new HashMap<String,String>();

    public static void getToken() throws ApiException {
        if(map.isEmpty()){
            DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
            OapiGettokenRequest request = new OapiGettokenRequest();
//            request.setAppkey("dingbad73751f4cad512");
//            request.setAppsecret("7Ee8jdF2r1P5SM58z7x-5BNTLXHCES85iZsMZ-ON0GmmgfoOdFw4Irq3ux729_VE");

//            request.setAppkey("dingb61c497ef76f4b33");
//            request.setAppsecret("-KNPdxB4qgN6oqBt1MU16EzOEH78Alz335LQCxNG1e6k68MCDITlJ9_IkWM1tJg9");

            request.setAppkey("ding2jbtro29lwzzb2yc");
            request.setAppsecret("o1rb2-sCsEapM1dEGdl1s_czRzjo_kGMRi8kX5Al3nwROLf8E_NVH4jO7qrxCl6e");


//            request.setAppkey("ding6b6edd50446cec2d");
//            request.setAppsecret("rdHJTrWRbA_u4UGjanoI-nP6JccQ6HFminewNDtwostGZ59u14uu-MyHxB5lgIbU");

            request.setHttpMethod("GET");
            OapiGettokenResponse response = client.execute(request);
            System.out.println("response==>{}"+response.getAccessToken());
            map.put("token",response.getAccessToken());
        }
        return ;
    }

    /**
     * 获取仟金顶的所有部门
     * @param args
     * @throws ApiException
     */
    public static void main(String[] args) throws ApiException {
//        getDepartments();
//         getAgentList();
//        sendMessage();
        sendOAMessage();
//        getsendresult();
//         getDepartments();
//         getUser();
//        getDeptMember();
//        getUser();
    }

    public static void getDepartments()throws ApiException{
        getToken();
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/department/list");
        OapiDepartmentListRequest request = new OapiDepartmentListRequest();
        request.setId("5906121");
        request.setHttpMethod("GET");
        OapiDepartmentListResponse response = client.execute(request, map.get("token"));
        System.out.println("department=>{}"+response);
    }

    public static void getDeptMember()throws ApiException{
        getToken();
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/getDeptMember");
        OapiUserGetDeptMemberRequest req = new OapiUserGetDeptMemberRequest();
        req.setDeptId("64609917");
        req.setHttpMethod("GET");
        OapiUserGetDeptMemberResponse rsp = client.execute(req, map.get("token"));
        System.out.println(rsp.getBody());
    }

    public static void getUser()throws ApiException{
        getToken();
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/get");
        OapiUserGetRequest request = new OapiUserGetRequest();
        request.setUserid("15514340426348096");
        request.setHttpMethod("GET");
        OapiUserGetResponse response = client.execute(request,  map.get("token"));
        System.out.println(response);
    }


    public static void sendMessage()throws ApiException{
        getToken();
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");

        OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
        request.setUseridList("15514340426348096");
        request.setAgentId(36537745L);
        request.setToAllUser(false);

        OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
        msg.setMsgtype("text");
        msg.setText(new OapiMessageCorpconversationAsyncsendV2Request.Text());
        msg.getText().setContent("test123");
        request.setMsg(msg);
        OapiMessageCorpconversationAsyncsendV2Response response = client.execute(request,map.get("token"));
        System.out.println(response);
    }

    public static  void sendNomarlMessage()throws ApiException{
        getToken();
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/message/send_to_conversation");

        OapiMessageSendToConversationRequest req = new OapiMessageSendToConversationRequest();
        req.setSender("15514340426348096");
        req.setCid("14ac70d94e79377b88aa5fc75759fe84");
        OapiMessageSendToConversationRequest.Msg msg = new OapiMessageSendToConversationRequest.Msg();

// 文本消息
        OapiMessageSendToConversationRequest.Text text = new OapiMessageSendToConversationRequest.Text();
        text.setContent("测试测试");
        msg.setText(text);
        msg.setMsgtype("text");
        req.setMsg(msg);
        OapiMessageSendToConversationResponse response = client.execute(req,map.get("token"));
        System.out.println(response);
    }

    public static void getAgentList()throws ApiException{
        getToken();
        DefaultDingTalkClient  client = new DefaultDingTalkClient("https://oapi.dingtalk.com/microapp/list");
        OapiMicroappListRequest req = new OapiMicroappListRequest();
        OapiMicroappListResponse response = client.execute(req,map.get("token"));
        System.out.println(response);
    }

    public static void sendOAMessage()throws ApiException{
        getToken();
        OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
        request.setUseridList("15577383919844573");
//        request.setAgentId(12045145L);

        request.setAgentId(660328540L);
        request.setToAllUser(false);
        OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
        msg.setOa(new OapiMessageCorpconversationAsyncsendV2Request.OA());
        msg.getOa().setHead(new OapiMessageCorpconversationAsyncsendV2Request.Head());
        msg.getOa().getHead().setText("head");
        msg.getOa().setBody(new OapiMessageCorpconversationAsyncsendV2Request.Body());
        msg.getOa().getBody().setContent("xxx");
        msg.setMsgtype("oa");
        request.setMsg(msg);
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");
        OapiMessageCorpconversationAsyncsendV2Response response  = client.execute(request,map.get("token"));
        System.out.println(response);
        //{"errcode":0,"task_id":135474603451,"request_id":"10cg0a291y2cz"}
    }

    public static void getsendprogress()throws ApiException{
        getToken();
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/getsendprogress");
        OapiMessageCorpconversationGetsendprogressRequest request  = new OapiMessageCorpconversationGetsendprogressRequest();
        request.setAgentId(12045145L);
        request.setTaskId(135641508287L);
        OapiMessageCorpconversationGetsendprogressResponse response = client.execute(request, map.get("token"));
        System.out.println(response);
    }

    public static void getsendresult()throws ApiException{
        getToken();
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/getsendresult");
        OapiMessageCorpconversationGetsendresultRequest request  = new OapiMessageCorpconversationGetsendresultRequest();
        request.setAgentId(12045145L);
        request.setTaskId(135474603451L);
        OapiMessageCorpconversationGetsendresultResponse response = client.execute(request, map.get("token"));
        System.out.println(response);
    }
}
